package sourceCode;

import java.awt.geom.Point2D;

import java.io.*;

import java.util.ArrayList;

import javax.xml.parsers.*;

import org.w3c.dom.*;

public class ParserXML {
    private DocumentBuilderFactory factory;
    private DocumentBuilder builder;
    private Document doc;
    private org.w3c.dom.Element nroot;

    /**
     *
     * @param path path of XML file
     * @throws Exception
     */
    public ParserXML(String cheminFic) {
        //Chargement du document
        try {
            factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
            doc = builder.parse(new File(cheminFic));
            this.nroot = doc.getDocumentElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Point2D.Double getTailleAeroport() {
        double longueur = Double.parseDouble(nroot.getAttribute("longueur"));
        double hauteur = Double.parseDouble(nroot.getAttribute("hauteur"));
        Point2D.Double taille = new Point2D.Double(longueur, hauteur);
        return taille;
    }

    public ArrayList<ZoneGarage> getGarages() {
        // Parcour du document
        NodeList listNoeudsGarages = this.nroot.getElementsByTagName("Garage");
        ArrayList<ZoneGarage> lGarages = new ArrayList<ZoneGarage>();
        for (int i = 0; i < listNoeudsGarages.getLength(); i++) {
            org.w3c.dom.Element e = (org.w3c.dom.Element)listNoeudsGarages.item(i);
            int id = Integer.parseInt(e.getAttribute("id"));
            double x = Double.parseDouble(e.getAttribute("x"));
            double y = Double.parseDouble(e.getAttribute("y"));
            Point2D.Double coords = new Point2D.Double(x, y);

            ZoneGarage garage = new ZoneGarage(id, coords);
            ArrayList<Chariot> lChariots = getChariotsGarages(e);
            garage.setListChariots(lChariots);
            int tailleIterator = lChariots.size();
            for (int j = 0; j < tailleIterator; j++) {
                lChariots.get(j).setZoneGarage(garage);
                garage.ajouterChariotXML(lChariots.get(j));
            }
            lGarages.add(garage);
        }
        return lGarages;
    }

    public ArrayList<Chariot> getChariotsGarages(org.w3c.dom.Element elemGarage) {
        NodeList listNoeudsCharGarage = elemGarage.getElementsByTagName("Chariot");
        ArrayList<Chariot> lChariots = new ArrayList<Chariot>();
        for (int i = 0; i < listNoeudsCharGarage.getLength(); i++) {
            org.w3c.dom.Element e = (org.w3c.dom.Element)listNoeudsCharGarage.item(i);
            int id = Integer.parseInt(e.getAttribute("id"));
            double x = Double.parseDouble(elemGarage.getAttribute("x"));
            double y = Double.parseDouble(elemGarage.getAttribute("y"));
            double vitesse = Double.parseDouble(e.getAttribute("vitesse"));
            Point2D.Double coords = new Point2D.Double(x, y);
            Chariot chariot = new Chariot(id, coords, vitesse);
            chariot.setPosition(Chariot.typePosition.garage);
            lChariots.add(chariot);
        }
        return lChariots;
    }
    // cette m�thode ne peut �tre appel�e qu'apr�s getGarages()

    public ArrayList<Chariot> getChariots(Aeroport aeroport) {
        ArrayList<Chariot> lChariots = new ArrayList<Chariot>();
        for (ZoneGarage garage : aeroport.getListeZonesGarages()) {
            for (Chariot chariot : garage.getListChariots()) {
                lChariots.add(chariot);
                chariot.setNoeud(garage);
            }
        }
        return lChariots;
    }

    public ArrayList<Embranchement> getEmbranchements() {
        // Parcour du document
        NodeList listNoeudsEmbranchements = this.nroot.getElementsByTagName("Embranchement");
        ArrayList<Embranchement> lEmbranchements = new ArrayList<Embranchement>();
        for (int i = 0; i < listNoeudsEmbranchements.getLength(); i++) {
            org.w3c.dom.Element e = (org.w3c.dom.Element)listNoeudsEmbranchements.item(i);
            int id = Integer.parseInt(e.getAttribute("id"));
            double x = Double.parseDouble(e.getAttribute("x"));
            double y = Double.parseDouble(e.getAttribute("y"));
            Point2D.Double coords = new Point2D.Double(x, y);

            Embranchement embranchement = new Embranchement(id, coords);
            lEmbranchements.add(embranchement);
        }
        return lEmbranchements;
    }

    public ArrayList<Guichet> getGuichets() {
        // Parcour du document
        NodeList listNoeudsGuichets = this.nroot.getElementsByTagName("Guichet");
        ArrayList<Guichet> lGuichets = new ArrayList<Guichet>();
        for (int i = 0; i < listNoeudsGuichets.getLength(); i++) {
            org.w3c.dom.Element e = (org.w3c.dom.Element)listNoeudsGuichets.item(i);
            int id = Integer.parseInt(e.getAttribute("id"));
            double x = Double.parseDouble(e.getAttribute("x"));
            double y = Double.parseDouble(e.getAttribute("y"));
            Point2D.Double coords = new Point2D.Double(x, y);
            double vitesseTapis = Double.parseDouble(e.getAttribute("vitesseTapis"));
            double longueurTapis = Double.parseDouble(e.getAttribute("longueurTapis"));
            double distanceSecurite = Double.parseDouble(e.getAttribute("distanceSecurite"));
            int nbBagagesMax = Integer.parseInt(e.getAttribute("nbBagagesMax"));
            Guichet guichet = new Guichet(id, coords, nbBagagesMax, vitesseTapis, longueurTapis, distanceSecurite);
            lGuichets.add(guichet);
        }
        return lGuichets;
    }

    public ArrayList<Tobogan> getToboggans() {
        // Parcour du document
        NodeList listNoeudsToboggans = this.nroot.getElementsByTagName("Toboggan");
        ArrayList<Tobogan> lToboggans = new ArrayList<Tobogan>();
        for (int i = 0; i < listNoeudsToboggans.getLength(); i++) {
            org.w3c.dom.Element e = (org.w3c.dom.Element)listNoeudsToboggans.item(i);
            int id = Integer.parseInt(e.getAttribute("id"));
            double x = Double.parseDouble(e.getAttribute("x"));
            double y = Double.parseDouble(e.getAttribute("y"));
            Point2D.Double coords = new Point2D.Double(x, y);
            Tobogan toboggan = new Tobogan(id, coords);
            lToboggans.add(toboggan);
        }
        return lToboggans;
    }


    //Une fois tous les noeuds r�cup�r�s, cette fonction parse le xml et r�cup�re chaque rail ainsi que son noeud de
    //d�part et d'arriv�e

    public ArrayList<Rail> getRails(Aeroport _aeroport) {
        // Parcour du document
        NodeList listNoeudsRails = this.nroot.getElementsByTagName("Rail");
        ArrayList<Rail> lRails = new ArrayList<Rail>();
        for (int i = 0; i < listNoeudsRails.getLength(); i++) {
            org.w3c.dom.Element e = (org.w3c.dom.Element)listNoeudsRails.item(i);
            int id = Integer.parseInt(e.getAttribute("id"));
            int idNoeudDepart = Integer.parseInt(e.getAttribute("idNoeudDep"));
            Noeud noeudDepart = _aeroport.getNoeudById(idNoeudDepart);
            int idNoeudArrive = Integer.parseInt(e.getAttribute("idNoeudArr"));
            double distanceSecurite = Double.parseDouble(e.getAttribute("distanceSecurite"));
            Noeud noeudArrive = _aeroport.getNoeudById(idNoeudArrive);
            Rail rail = new Rail(id, new Point2D.Double(), noeudDepart, noeudArrive, distanceSecurite);
            lRails.add(rail);
        }
        return lRails;
    }
}
